#include<iostream>
#include<vector>
#include<map>

using namespace std;

vector<string> parse(string s)
{
    int prev = 0;
    vector<string> res;
    for (int i = 0; i <= s.size(); i++)
        if (i == s.size() || s[i] == ' ')
        {
            res.push_back(s.substr(prev, i - prev));
            prev = i + 1;
        }
    return res;
}
bool wordPattern(string pattern, string s) {
    auto words = parse(s);
    if (words.size() != pattern.size())  return false;
    map<string, char> m1;
    map<char, string> m2;
    for (int i = 0; i < pattern.size(); i++)
    {
        char c = pattern[i];
        string word = words[i];
        if (m1[word] > 0 && m1[word] != c || m2[c] != "" && m2[c] != word)
            return false;
        m1[word] = c;
        m2[c] = word;
    }
    return true;
}